package me.luger.project.fof.service;

import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.fof.entity.UserInfo;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserInfoService extends BaseService<UserInfo> {
    public UserInfoService(Dao dao) {
        super(dao);
    }

    public UserInfo detail(Integer id){
        Sql sql = Sqls.create("select u.*,c.`name` as companyName from fof_user_info u left join fof_company c on u.companyId = c.id where u.id = " + id);
        List<UserInfo> recordList = this.list(sql);
        if(null != recordList && recordList.size() == 1){
            return recordList.get(0);
        }
        return null;
    }

    public UserInfo login(String userName,String password){
        Sql sql = Sqls.create("select * from fof_user_info where name = @name and password = @password");

        sql.setParam("name",userName);
        sql.setParam("password",password);

        UserInfo userInfo = this.fetchOne(sql);
        return userInfo;
    }

    public List<UserInfo> searchByName(String userName){
        Sql sql = Sqls.create("select a.id,a.name,b.name as companyName from fof_user_info a left join fof_company b on a.companyId = b.id where a.name like @userName");
        sql.setParam("userName","%"+userName+"%");

        List<UserInfo> userInfoList = this.list(sql);
        return userInfoList;
    }
}
